package com.zhou.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhou.config.Result;
import com.zhou.entity.QuesCategory;
import com.zhou.mapper.QuesCategoryMapper;
import com.zhou.service.QuesCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 题目分类表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2023-03-02
 */
@RestController
@RequestMapping("/question")
@Api(tags = "题目分类表接口信息")
public class QuesCategoryController {

    @Autowired
    private QuesCategoryService quesCategoryService;

    @Resource
    private QuesCategoryMapper quesCategoryMapper;

    private static final Integer SIZE = 6;

    //
    //输入localhost:8090/question/findCategoryById/1
    //返回：
    // {"code":"0","msg":"成功","data":{"categoryId":1,"categoryName":"选择题","repoId":1,"createTime":"2023-03-03T00:27:28","updateTime":"2023-03-03T00:27:28"}}


    //呈现出此表的所有信息
    @ApiOperation(value = "呈现出此表的所有信息")
    @GetMapping("/findAllCategory")
    public List<QuesCategory> findAllCategory(){
        return this.quesCategoryMapper.selectList(null);
    }


   
    //增加一条完整的记录
    @ApiOperation(value = "增加一条完整的题目分类表的记录")
    @RequestMapping(value = "/addCategory",method = RequestMethod.PUT)
    public int add(@RequestParam(name = "repo_id")int repo_id,
                      @RequestParam(name = "category_name")String category_name,
                      @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
                      @RequestParam(name = "create_time")LocalDateTime create_time,
                      @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
                      @RequestParam(name = "update_time")LocalDateTime update_time
                      ){
        QuesCategory quesCategory = new QuesCategory();
        quesCategory.setCategoryName(category_name);
        quesCategory.setRepoId(repo_id);
        quesCategory.setCreateTime(create_time);
        quesCategory.setUpdateTime(update_time);
        return this.quesCategoryMapper.insert(quesCategory);
    }

    //修改记录信息
    @ApiOperation(value = "根据category_id修改记录信息")
    @PostMapping(value = "/updateCategory")
    public int update(@RequestParam(value = "category_id") int category_id,
                         @RequestParam(value = "category_name") String category_name,
                         @RequestParam(value = "repo_id")int repo_id,
                         @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
                             @RequestParam(name = "update_time")LocalDateTime update_time){
        QuesCategory quesCategory = new QuesCategory();
        quesCategory.setCategoryId(category_id);
        quesCategory.setCategoryName(category_name);
        quesCategory.setRepoId(repo_id);
        quesCategory.setUpdateTime(update_time);
        return this.quesCategoryMapper.updateById(quesCategory);
    }

    //模糊删除记录信息
    @ApiOperation(value = "模糊删除记录")
    @DeleteMapping(value = "/deleteCategory")
    public void delete(@RequestParam(value = "category_id",required = false) Integer category_id,
                       @RequestParam(value = "category_name",required = false) String category_name,
                       @RequestParam(value = "repo_id",required = false) Integer repo_id){
        QueryWrapper<QuesCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(category_id!=null,"category_id", category_id)
                .like(category_name!= null,"category_name",category_name)
                .like(repo_id!=null,"repo_id",repo_id);
        quesCategoryMapper.delete(queryWrapper);
    }

    //准确删除记录
    @ApiOperation(value = "准确删除记录")
    @DeleteMapping(value = "/deleteAcu")
    public void deleteAcu(@RequestParam(value = "category_id",required = false,defaultValue = "0") int category_id,
                       @RequestParam(value = "category_name",required = false) String category_name,
                       @RequestParam(value = "repo_id",required = false,defaultValue = "0") int repo_id){
        if (category_id == 0){
           if(repo_id == 0){
               quesCategoryMapper.deleteByName(category_name);
           }
           else if (category_name!=null) {
               quesCategoryMapper.deleteByReIdAndName(repo_id,category_name);
           }
           else {
               quesCategoryMapper.deleteByReId(repo_id);
           }
       }
        else if (repo_id == 0){
                quesCategoryMapper.deleteById(category_id);
        }
        else {
            quesCategoryMapper.deleteByIdAndNameAndReId(category_id,category_name,repo_id);
        }
    }
}

